<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.bTime"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.eTime"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('商品分类')">
              <a-tree-select
                allowClear
                v-model="queryParam.kindId"
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="treeData"
                :search-placeholder="$t('请选择')"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="12" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-dropdown>
                <a-menu slot="overlay" @click="handleDateClick">
                  <a-menu-item key="1"> {{$t('今日')}} </a-menu-item>
                  <a-menu-item key="2"> {{$t('近三天')}} </a-menu-item>
                  <a-menu-item key="3"> {{$t('近七天')}} </a-menu-item>
                  <a-menu-item key="4"> {{$t('近一月')}} </a-menu-item>
                  <a-menu-item key="5"> {{$t('近三月')}} </a-menu-item>
                  <a-menu-item key="6"> {{$t('近半年')}} </a-menu-item>
                  <a-menu-item key="7"> {{$t('本年')}} </a-menu-item>
                </a-menu>
                <a-button type="primary" style="margin-left: 8px"> {{ $t('快捷查询') }} <a-icon type="down" /></a-button>
              </a-dropdown>
              <a-button type="primary" style="margin-left: 8px" @click="searchReset">{{
                $t('重置')
              }}</a-button>
              <a-button type="primary" :loading="exloading" style="margin-left: 8px" @click="handleExportXls($t('商品分类汇总'))" v-has="'GoodsKindSummaryController:export'">{{
                $t('导出')
              }}</a-button>
            </span>
          </a-col>
          <template v-if="toggleSearchStatus"></template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="kindId"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'GoodsKindSummary',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      moment,
      description: '商品分类汇总页面',
      // 表头
      columns: [
        {
          title: this.$t('分类编码'),
          align: 'center',
          dataIndex: 'kindCode',
          customRender:(t,r)=>{
            if(t=='合计'){
              return this.$t(t)
            }
            return t
          }
        },
        {
          title: this.$t('分类名称'),
          align: 'center',
          dataIndex: 'kindName',
        },
        {
          title: this.$t('业务笔数'),
          align: 'center',
          dataIndex: 'count',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('成本'),
          align: 'center',
          dataIndex: 'cost',
          customRender: (t) => {
            if (!t) {
              t = 0
            }
            return t.toFixed(4)
          },
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('销售金额'),
          align: 'center',
          dataIndex: 'totle',
          customRender: (t) => {
            if (!t) {
              t = 0
            }
            return t.toFixed(2)
          },
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('利润'),
          align: 'center',
          dataIndex: 'profit',
          customRender: (t) => {
            if (!t) {
              t = 0
            }
            return t.toFixed(2)
          },
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('利润占比'),
          align: 'center',
          dataIndex: 'ratio',
          customRender: (t,r) => {
            if(!t){
              return ''
            }
            return t + '%'
          },
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        },
      ],
      dataSource: [],
      queryParam: {
        bTime: moment().format('YYYY-MM-DD 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
        kindId: '',
      },
      url: {
        list: '/report/goods/goodsKindSummary/list',
        exportXlsUrl: '/report/goods/goodsKindSummary/export',
      },
      treeData: [],
      allChildKind: [],
      isCustom: true,
    }
  },
  mounted() {
    this.select()
  },
  methods: {
    customRow(r, i) {
      return {
        on:{
          click: () => {
            let x = this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i})
            if( x == -1 ){
              this.selectedRowIndex.push('a'+this.ipagination.current+''+i)
            }else{
              this.selectedRowIndex.splice(x, 1)
            }
          }
        },
        style: {
          whiteSpace: 'break-spaces',
          wordBreak: 'break-word',
          overflowWrap: 'break-word',
          backgroundColor: this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i}) > -1 ? '#FFECE6' : '',
          fontWeight: r.kindCode == '合计' ? 700 : 400,
        },
      }
    },
    initDictConfig() {
      this.searchQuery()
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      getAction('/goods/bisGoodsKind/rootList', param).then((res) => {
        if (res.success) {
          that.allChildKind = []
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            that.treeData = that.getDataByResult(res.result.records)
            that.treeData.unshift({ key: '', id: '', value: '', title: that.$t('全部') })
          } else {
            that.treeData = []
          }
        } else {
          that.$alert(res.message, that.$t('提示'), {
            confirmButtonText: that.$t('确定'),
            callback: (action) => {},
          })
        }
      })
    },
    getHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      getAction('/goods/bisGoodsKind/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.treeData = [...that.treeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      let that = this
      return result.map((item) => {
        if (item.hasChild == 0 && item.key != '0') {
          that.allChildKind.push(item.key)
        }
        return item
      })
    },
    searchReset() {
      this.queryParam = {
        bTime: moment().format('YYYY-MM-DD 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
        kindId: '',
      }
      this.searchQuery()
    },
    handleDateClick(evt){
      if(evt.key=='1'){
        this.queryParam.bTime=moment().format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='2'){
        this.queryParam.bTime=moment().subtract(2,'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='3'){
        this.queryParam.bTime=moment().subtract(6,'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='4'){
        this.queryParam.bTime=moment().subtract(1, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='5'){
        this.queryParam.bTime=moment().subtract(3,'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='6'){
        this.queryParam.bTime=moment().subtract(6,'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='7'){
        this.queryParam.bTime=moment().startOf('year').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>